package com.product.web;

import com.product.exception.AppException;
import com.product.exception.SelectNotException;
import com.product.pojo.Login;
import com.product.service.LoginService;
import com.product.service.impl.LoginServiceUImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet({"/login","/register","/quit","/modify"})
public class LoginServlet extends HttpServlet {
    LoginService loginService=new LoginServiceUImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取servlet路劲
        String servletPath=request.getServletPath();
        if ("/login".equals(servletPath)){
            doLogin(request,response);
        } else if ("/register".equals(servletPath)) {
            doRegister(request,response);
        } else if ("/quit".equals(servletPath)) {
            doQuit(request,response);
        } else if ("/modify".equals(servletPath)) {
            doModify(request,response);

        }

    }

    private void doModify(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
            String email=request.getParameter("email");
            String password=request.getParameter("confirm-new-password");
            Login login=new Login();
            login.setEmail(email);
            login.setPassword(password);
        try {
            int count=loginService.modifyPassword(login);
            if (count>0){
                //说明修改成功了
                response.sendRedirect(""+request.getContextPath()+"/modifySuccess.jsp");
            }
        } catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }

    }

    private void doQuit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取session
        HttpSession session=request.getSession(false);
        //销毁sessio
        session.invalidate();
        response.sendRedirect(""+request.getContextPath()+"/login.jsp");

    }

    private void doRegister(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取数据
        String username=request.getParameter("username");
        String email=request.getParameter("email");
        String password=request.getParameter("password");
        Login login=new Login();
        login.setUsername(username);
        login.setEmail(email);
        login.setPassword(password);
        try {
            int count=loginService.registerUser(login);
            if (count==1){
                //注册成功
                response.sendRedirect(""+request.getContextPath()+"/regiserSuccess.jsp");

            }else {
                response.sendRedirect("" + request.getContextPath() + "/error.jsp");
            }
        } catch (SelectNotException | AppException e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");
        }

    }

    private void doLogin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            int count = loginService.findUser(username, password);
            if (count==1){
                //登录成功了
                //用session机制来防止不登录直接跳转到增删页面
                HttpSession session=request.getSession();
                session.setAttribute("username",username);
                response.sendRedirect("" + request.getContextPath() + "/findAll");
            }else {
                response.sendRedirect("" + request.getContextPath() + "/loginError.jsp");
            }
        } catch (Exception e) {
            response.sendRedirect("" + request.getContextPath() + "/error.jsp");


        }
    }

}
